<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Topic_model extends CI_Model
{
    private $topic_table_name   = 'topic';
    private $post_table_name   = 'post';
    private $topic_category_table_name   = 'topic_category';
    private $users_table_name   = 'users';
    private $error = array();
    
    function __construct()
    {
        parent::__construct();
        $ci =& get_instance();
    }
    
    function get_all_topic(){
        $this->db->select('*');
        $this->db->from($this->topic_table_name);
        $this->db->where('status', 1);
        $this->db->order_by("modified_time", "desc"); 
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_all_topic_by_user_id($user_id){
        $this->db->select('*');
        $this->db->select('topic.id as topic_id,users.id as user_id');
        //$this->db->join($this->post_category_table_name, $this->post_category_table_name.'.category_key = '.$this->post_table_name.'.post_category');
        $this->db->join($this->users_table_name, $this->users_table_name.'.id = '.$this->topic_table_name.'.user_id');
        $this->db->from($this->topic_table_name);
        $this->db->where($this->topic_table_name.'.user_id',$user_id);
        $this->db->order_by("modified_time", "desc"); 
        $query = $this->db->get();
        return $query->result_array();
    }

    function save($data,$user_id,$status=1){
        
        $remove_keys = array('file'=>'','save'=>'');
        $data['user_id'] = $user_id;
        $data['upload_image'] = isset($data['upload_image'])?json_encode($data['upload_image']):'';
        $data = array_diff_key($data, $remove_keys);
        $data['modified_time'] = date('Y-m-d H:i:s', time());
        $data['status'] = $status;
        $this->db->insert($this->topic_table_name, $data);
        
        if($this->db->affected_rows() > 0 ) return true;
        else return false;
    }

    function get_recent_topic(){
        $this->db->select('*');
        $this->db->select('topic.id as topic_id');
        $this->db->join($this->users_table_name, $this->users_table_name.'.id = '.$this->topic_table_name.'.user_id');
        $this->db->from($this->topic_table_name);
        $this->db->limit(20);
        $this->db->order_by("modified_time", "desc"); 
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_topic_by_id($topic_id){
        
        $this->db->select('*');
        $this->db->join($this->topic_category_table_name, $this->topic_category_table_name.'.category_value = '.$this->topic_table_name.'.topic_category');
        $this->db->from($this->topic_table_name); 
        $this->db->where($this->topic_table_name.'.id', $topic_id);
        $query = $this->db->get();

        return $query->result_array();
    }

    function get_previous_next_by_current_id($topic_id){
        $post = array();
        //next record
        $this->db->select('*')->from($this->topic_table_name);
        $this->db->where('`id` = (SELECT MIN(id) FROM `'.$this->topic_table_name.'` where `id` > '.$topic_id.') AND `status`=1', NULL, FALSE);
        $query = $this->db->get();
        $post['next'] = $query->result_array();

        //previous

        $this->db->select('*')->from($this->topic_table_name);
        $this->db->where('`id` = (SELECT MAX(id) FROM `'.$this->topic_table_name.'` where `id` < '.$topic_id.') AND `status`=1', NULL, FALSE);
        $query = $this->db->get();
        $post['previous'] = $query->result_array();
        
        
        return $post;

    }

    function get_topic_by_id_string($topic_id_string){
        $post = array();
        $this->db->select('*');
        $this->db->from($this->topic_table_name);
        $this->db->where('status', '1');
        $this->db->where_in('id',$topic_id_string);
        $query = $this->db->get();
        
        $post = $query->result_array();
        return $post;
    }

    function modify($data,$topic_id,$status=1){
        $remove_keys = array('file'=>'','save'=>'','topic_id'=>'');
        $data['upload_image'] = isset($data['upload_image'])?json_encode($data['upload_image']):'';
        $data['status'] = '1';//set draft to published
        $data = array_diff_key($data, $remove_keys);
        
        $data['modified_time'] = date('Y-m-d H:i:s', time());
        $data['status'] = $status;
        $this->db->where('id', $topic_id);
        $this->db->update($this->topic_table_name, $data);
        
        if($this->db->affected_rows() > 0 ) return true;
        else return false;
    }

    function delete($topic_id){
        $this->db->delete($this->topic_table_name, array('id' => $topic_id));
        if($this->db->affected_rows() > 0 ) return true;
        else return false;    
    }

    function get_topic_by_offset($offset,$number){
        $this->db->select('*');
        $this->db->select('topic.id as topic_id');
        $this->db->from($this->topic_table_name);
        $this->db->where('status','1');
        $this->db->limit( $number, $offset);
        $this->db->order_by("modified_time", "desc"); 
        $query = $this->db->get();
        return $query->result_array();
    }
    function get_topic_by_keyword($keyword,$offset,$number){

        $kwds = explode(" ", $keyword);
        $kwds = array_map("mysql_real_escape_string", $kwds);
        $regexp = implode("|", $kwds) ;

        $regexp_all_match = '';//= implode(".*", $kwds) ;

        foreach ($kwds as $key => $value) {
            $regexp_all_match .= '(?=.*'.$value.')';
        }
        
        $this->db->select('*,topic.id as topic_id');
        $this->db->from($this->topic_table_name);
        //$where = "(status = '1') AND (topic_category like '%".$keyword."%' OR topic_desc like '%".$keyword."%' OR topic_tags like '%".$keyword."%' OR topic_excerpt like '%".$keyword."%')";
        $where = "(status = '1') AND (LOWER(`topic_category`) REGEXP '".$regexp."' OR LOWER(`topic_desc`) REGEXP '".$regexp."' OR LOWER(`topic_excerpt`) REGEXP '".$regexp."')";
        $this->db->where($where);
        $this->db->limit( $number, $offset);
        //$this->db->order_by("modified_time", "desc"); 
        $query = $this->db->get();
        return $query->result_array();
    }

}
